/*

Sript que se encarga de crear tabla donde se mostrara la lista de clientes

*/
Ext.Loader.setConfig({enabled: true});


Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.toolbar.Paging',
    'Ext.ModelManager',
    'Ext.tip.QuickTipManager'
]);

var win,button;
var createWindowForm;
var store;

Ext.onReady(function(){
    Ext.tip.QuickTipManager.init();
	button = Ext.get('new-btn');
	//Agrega listener al boton, crear usuario
	button.on('click', createWindowForm)

	
	//Define el modelo que se utilizara para guardar los datos de los usuarios
    Ext.define('User', {
        extend: 'Ext.data.Model',
        fields: [
			{name: 'id', type: 'int'},
			{name: 'num_fidelidad', type: 'int'},
            'nombres', 'apellidos', 'telefono', 'domicilio','email',
            
            {name: 'fecha_nacimiento', mapping: 'fecha_nacimiento', type: 'date'},
            'genero', 'ocupacion'
        ],
        idProperty: 'id'
    });

    // create the Data Store, almacena los datos provemientes del servidor
    store = Ext.create('Ext.data.Store', {
        pageSize: 50,
        model: 'User',
        remoteSort: true,
        proxy: {
            // load using script tags for cross domain, if the data in on the same domain as
            // this page, an HttpProxy would be better
            type: 'ajax',
            url: 'admin_cliente.php?accion=list',
            reader: {
                root: 'data',
                totalProperty: 'totalCount'
            },
            // sends single sort as multi parameter
            simpleSortMode: true
        },
        sorters: [{
            property: 'lastpost',
            direction: 'DESC'
        }]
    });


    //Tabla que muestra los clientes
    var grid = Ext.create('Ext.grid.Panel', {
        width: 1000,
        height: 500,
        title: 'Clientes',
        store: store,
        disableSelection: true,
        loadMask: true,
        viewConfig: {
            id: 'gv',
            trackOver: false,
            stripeRows: false
        },
        // grid columns
        columns:[{
            text: "Id",
            dataIndex: 'id',
            width: 50,
            sortable: true
        },{
            text: "Numero de Fidelidad",
            dataIndex: 'num_fidelidad',
            width: 100,
            sortable: true
        },{
            text: "Nombres",
            dataIndex: 'nombres',
            width: 100,
            sortable: true
        },{
            text: "Apellidos",
            dataIndex: 'apellidos',
            width: 70,           
            sortable: true
        },{
            text: "Email",
            dataIndex: 'email',
            width: 70,           
            sortable: true
        },
            {
                xtype: 'actioncolumn',
                width: 50,
                items: [{
                    icon   : '../shared/icons/fam/user_edit.png',  // Use a URL in the icon config
                    tooltip: 'edit',
                    handler: //function(grid, rowIndex, colIndex) {
                        //var rec = store.getAt(rowIndex);
                        //alert("Sell " + rec.get('company'));
						createWindowForm//()
						
                    //}
                },
				{
                    icon   : '../shared/icons/fam/delete.gif',  // Use a URL in the icon config
                    tooltip: 'delete',
                    handler: function(grid, rowIndex, colIndex) {
					//alert("fadfs")
						var userModel = store.getAt(rowIndex);			
						id=userModel.get("id");
						
						Ext.Ajax.request({
							params: {id:id},
							url: 'admin_cliente.php?accion=del',
							success: function (resp) {
								var data;
								data = Ext.decode(resp.responseText);
								if (data.success == "true") {
									Ext.MessageBox.alert('Message', data.success);
									store.loadPage(store.currentPage);
								} else {
									Ext.MessageBox.alert('Error', 'Some problem occurred 1');
								}
							},
							failure: function () {
								Ext.MessageBox.alert('Error', 'Some problem occurred 2');
							}
						});
						
                    }
                }
				]
			}
		
		
		],
        // paging bar on the bottom
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: 'Clientes {0} - {1} of {2}',
            emptyMsg: "No hay Clientres",
            items:[]
        }),
        renderTo: 'topic-grid'
    });

    // trigger the data store load
    store.loadPage(1);
});



